<?php
namespace backend\components\widget\adminLTE;

use yii\helpers\Html;

/**
 * Class ActiveField
 * @package backend\components\widget\adminLTE
 */
class ActiveField extends \yii\widgets\ActiveField
{

    /**
     * @var string this property holds a custom input id if it was set using [[inputOptions]] or in one of the
     * `$options` parameters of the `input*` methods.
     */
    private $_inputId;
    /**
     * @var bool if "for" field label attribute should be skipped.
     */
    private $_skipLabelFor = false;

    /**
     * Generates a label tag for [[attribute]].
     * @param null|string|false $label the label to use. If `null`, the label will be generated via [[Model::getAttributeLabel()]].
     * If `false`, the generated field will not contain the label part.
     * Note that this will NOT be [[Html::encode()|encoded]].
     * @param null|array $options the tag options in terms of name-value pairs. It will be merged with [[labelOptions]].
     * The options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded
     * using [[Html::encode()]]. If a value is `null`, the corresponding attribute will not be rendered.
     * @return $this the field object itself.
     */
    public function label($label = null, $options = [])
    {
        if ($label === false) {
            $this->parts['{label}'] = '';
            return $this;
        }

        $options = array_merge($this->labelOptions, $options);
        if ($label !== null) {
            $options['label'] = $label;
        } else {
            $options['label'] = Html::encode($this->model->getAttributeLabel($this->attribute));
        }

        if ($this->_skipLabelFor) {
            $options['for'] = null;
        }

        $options['label'] .= $this->helperHtml($options);

        $this->parts['{label}'] = Html::activeLabel($this->model, $this->attribute, $options);

        return $this;
    }

    /**
     * helperHtml
     * @param $options
     * @return string
     */
    private function helperHtml(&$options)
    {
        $html = '';
        //短 的 注释
        if(isset($options['tinytext'])){
            $html = '<span class="help-text">' . $options['tinytext'] . '</span>';
            unset($options['tinytext']);
        }
        return $html;
    }


    /**
     * rightSpan
     * @param string $text
     * @param array $options
     * @return $this
     */
    public function rightSpan($text='',$options=[])
    {

        $options['class'] = 'input-group-addon '.($options['class']??'');

        $spanHtml = Html::tag('span',$text,$options);

        $inputTemplate = '<div class="input-group input-group-sm">{input}'.$spanHtml.'</div>';

        $this->parts['{input}'] = strtr($inputTemplate, ['{input}' => $this->parts['{input}']]);

        return $this;
    }

    /**
     * rightButton
     * @param string $text
     * @param array $options
     * @return $this
     */
    public function rightButton($text='',$options=[])
    {
        if(isset($options['icon'])){
            $text = '<i class="'.$options['icon'].'"></i> '.$text;
        }

        $options['class'] = 'btn btn-default '.($options['class']??'');

        $buttonHtml = Html::button($text,$options);

        $inputTemplate = '<div class="input-group input-group-sm">{input}<span class="input-group-btn">'.$buttonHtml.'</span></div>';

        $this->parts['{input}'] = strtr($inputTemplate, ['{input}' => $this->parts['{input}']]);

        return $this;
    }

}